
package com.kweative.model.column;

import com.kweative.crypto.StringEncrypter;
import com.kweative.crypto.StringEncrypter.EncryptionException;
import com.kweative.model.Table;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author henrik
 */
public class ColumnPassword<T extends Table> extends ColumnText<T> {
    StringEncrypter encryption = null;
    public ColumnPassword(String name) {
        this();
		setName(name);
    }

	public ColumnPassword() {
		super(120);
		setSQLNotNull(true);
        try {
            encryption = new StringEncrypter(StringEncrypter.DESEDE_ENCRYPTION_SCHEME);
        } catch (EncryptionException ex) {
            Logger.getLogger(ColumnPassword.class.getName()).log(Level.SEVERE, null, ex);
        }
	}


    @Override
    public String serialize(Object value) {
        if (value == null) return null;
        try {
            return super.serialize(encryption.encrypt((String) value));
        } catch (EncryptionException ex) {
            Logger.getLogger(ColumnPassword.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public String unserialize(String value) {
        if (value == null || value.length() == 0) return null;
        try {
            return encryption.decrypt(value);
        } catch (EncryptionException ex) {
            Logger.getLogger(ColumnPassword.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
